package com.cloudptt.api.product.func.media;

import com.cloudptt.api.product.func.media.device.DeviceKeyInfo;
import com.cloudptt.api.product.func.media.device.IDeviceKey;
import com.cloudptt.api.product.log.Log;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class MediaBroadcast extends BroadcastReceiver
{
	private Context ctx = null;
	private EventListener listener = null;
	private MediaFunc agent = null;
	private IDeviceKey devKey = null;
	
	
	public MediaBroadcast(Context ctx, MediaFunc agent)
	{
		this.ctx = ctx;
		this.agent = agent;
		devKey = DeviceKeyInfo.getInfo();
		IntentFilter filter = devKey.getBstIntentFilter();
		if(filter != null){
			ctx.registerReceiver(this, filter);
		}
	}

	public void setlistener(EventListener listener) {
		this.listener = listener;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.debug(this.getClass(), "onReceive: action = " + action);
		if(devKey == null)		{
			devKey = DeviceKeyInfo.getInfo();
		}
		if(devKey != null){
			devKey.procBst(intent, listener);
		}
	}

	public void clearResource()
	{
		this.ctx.unregisterReceiver(this);
		this.ctx = null;
		this.listener = null;
		this.agent = null;
	}
}
